#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 01-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Collections;
#endregion

namespace Thn.Interface.Vcl.Themes
{
    /// <summary>
    /// A simple implementation of <see cref="IThemeManager"/>.
    /// </summary>
#if DEBUG
    public
#else
    internal
#endif
 class SimpleThemeManager : IThemeManager
    {
        #region Fields
        /// <summary>
        /// Cache of themes by ID
        /// <para>key   : ID (string, lowercased)</para>
        /// <para>value : ITheme</para>
        /// </summary>
        Hashtable mThemes = new Hashtable();
        #endregion

        #region IThemeManager Members

        #region Current
        private ITheme mCurrent;
        /// <summary>
        /// Gets the default theme.
        /// </summary>
        public ITheme Current
        {
            get { return mCurrent; }
        }
        #endregion

        #region Use Theme
        /// <summary>
        /// Set a theme as the current theme.
        /// </summary>
        /// <param name="id">ID of the theme.</param>
        public void UseTheme(string id)
        {
            ITheme theme = this[id.ToLower()];
            if (theme != null)
            {
                mCurrent = theme;
            }
            else
            {
                throw new Exception(string.Format("Could not find theme with ID: {0}", id));
            }
        }

        /// <summary>
        /// Set a theme as the current theme.
        /// </summary>
        /// <param name="theme">The theme to be used.</param>
        public void UseTheme(ITheme theme)
        {
            //override existing if available
            this[theme.Name.ToLower()] = theme;

            //set as current
            mCurrent = theme;
        }
        #endregion

        #region Indexer
        /// <summary>
        /// Gets/Sets a theme based on its id.
        /// </summary>
        public ITheme this[string id]
        {
            get
            {
                ITheme result = null;
                string key = id.ToLower();
                if (mThemes.Contains(key))
                {
                    result = (ITheme)mThemes[key];
                }
                #region Defensive tracing
                else
                {
                    //not found
                }
                #endregion

                return result;
            }
            set
            {
                string key = id.ToLower();
                mThemes[key] = value;

                //assign as default theme if this is the only theme.
                if (mThemes.Count == 1)
                {
                    UseTheme(key);
                }
                #region Defensive tracing
                else
                {
                    //has more than 2 themes, no need to set default.
                }
                #endregion
            }
        }
        #endregion

        #region Contains
        /// <summary>
        /// Checks whether a theme ID is valid.
        /// </summary>
        public bool Contains(string themeId)
        {
            return mThemes.Contains(themeId.ToLower());
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public SimpleThemeManager()
        {
        }
        #endregion
    }
}
